package gold.digger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC641 {
    public long startExecuteTime = System.currentTimeMillis();


    class MyCircularDeque {
        private int[] queue;
        private int front;
        private int rear;
        private int capacity;

        /**
         * Initialize your data structure here. Set the size of the deque to be k.
         */
        public MyCircularDeque(int k) {
            this.queue = new int[k + 1];
            this.capacity = k + 1;
            this.front = 0;
            this.rear = 0;
        }

        /**
         * Adds an item at the front of Deque. Return true if the operation is successful.
         */
        public boolean insertFront(int value) {
            if (!this.isFull()) {
                this.front = (this.front - 1 + this.capacity) % this.capacity;
                this.queue[this.front] = value;
                return true;
            }
            return false;
        }

        /**
         * Adds an item at the rear of Deque. Return true if the operation is successful.
         */
        public boolean insertLast(int value) {
            if (!isFull()) {
                this.queue[this.rear] = value;
                this.rear = (this.rear + 1) % this.capacity;
                return true;
            }
            return false;
        }

        /**
         * Deletes an item from the front of Deque. Return true if the operation is successful.
         */
        public boolean deleteFront() {
            if (!this.isEmpty()) {
                this.front = (this.front + 1) % this.capacity;
                return true;
            }
            return false;
        }

        /**
         * Deletes an item from the rear of Deque. Return true if the operation is successful.
         */
        public boolean deleteLast() {
            if (!this.isEmpty()) {
                this.rear = (this.rear - 1 + this.capacity) % this.capacity;
                return true;
            }
            return false;
        }

        /**
         * Get the front item from the deque.
         */
        public int getFront() {
            if (!this.isEmpty()) {
                return this.queue[this.front];
            }
            return -1;
        }

        /**
         * Get the last item from the deque.
         */
        public int getRear() {
            if (!this.isEmpty()) {
                return this.queue[(this.rear - 1 + this.capacity) % this.capacity];
            }
            return -1;
        }

        /**
         * Checks whether the circular deque is empty or not.
         */
        public boolean isEmpty() {
            if (this.front == this.rear) {
                return true;
            }
            return false;
        }

        /**
         * Checks whether the circular deque is full or not.
         */
        public boolean isFull() {
            if ((this.rear + 1) % this.capacity == this.front) {
                return true;
            }
            return false;
        }
    }

    /**
     * Your MyCircularDeque object will be instantiated and called as such:
     * MyCircularDeque obj = new MyCircularDeque(k);
     * boolean param_1 = obj.insertFront(value);
     * boolean param_2 = obj.insertLast(value);
     * boolean param_3 = obj.deleteFront();
     * boolean param_4 = obj.deleteLast();
     * int param_5 = obj.getFront();
     * int param_6 = obj.getRear();
     * boolean param_7 = obj.isEmpty();
     * boolean param_8 = obj.isFull();
     */

    public void run() {
        MyCircularDeque solution = new MyCircularDeque(5);
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC641 an = new LC641();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
